Home | | Latest | About | Random
# Join and Concatenate answers.
```cpp
#include
#include
using namespace std;
/**
Prints the elements of a vector of ints, like { 3, 1, 4 }.
@param v : the vector to print
*/
void print(vector& v) {
if (v.empty()) {
cout << "{ }" << endl;
return;
}
cout << "{ ";
for (size_t i = 0; i < v.size() - 1; ++i) {
cout << v[i] << ", ";
}
cout << v[v.size() - 1] << " }" << endl;
}
/**
Joins together two vectors of ints
@param a : the first vector of ints
@param b : the second vector of ints
@return : Gives a new vector of ints
with entries from a followed by b
*/
vector join(const vector& a, const vector& b) {
vector result;
for(size_t i = 0; i < a.size() ; ++i) {
result.push_back(a.at(i));
}
for(size_t i = 0; i < b.size() ; ++i) {
result.push_back(b.at(i));
}
return result;
}
/**
Concatenates the second vector of ints to the first,
where the first vector of ints is modified.
@param a : the first vector of ints
@param b : the second vector of ints
*/
void cat(vector& a, const vector& b) {
size_t N = b.size();
for (size_t i = 0 ; i < N ; ++i) {
a.push_back(b.at(i));
}
}
int main() {
vector a(5, 1);
vector b(3, 10);
vector c = join(a,b);
print(c);
cat(c, c);
print(c);
return 0;
}
```